<template>
  <div style="display: flex;">
    <el-form-item  label="维修记录搜索：">
      <div style="display: flex;">
        <el-input v-model="findparams.keyword" 
        placeholder="输入手机号/维修类型/维修地址进行查询"
        style="width: 300px;"></el-input>
        <el-button type="primary" plain @click="getTableData" size="large" style="margin-left: 10px; margin-right: 10px;">搜索</el-button>
         <el-select
            v-model="findparams.part"
            placeholder="选择查询类型"
            size="large"
            style="width: 240px"
            @change="getTableData">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
        </el-select>
        <el-button type="primary" plain @click="changeOrder" size="large" style="margin-left: 10px; width: 130px;">
          <p v-if="!findparams.order">按时间排序</p>
          <p v-else>恢复默认排序排序</p>
        </el-button>
      </div>
    </el-form-item>
  </div>
  <el-table :data="tableData.records" style="width: 100% ;height: 77vh;">
    <el-table-column prop="type" label="报修类型" width="auto"/>
    <el-table-column prop="repairTime" label="报修时间" width="auto"/>
    <el-table-column prop="phone" label="联系方式" width="auto" />
    <el-table-column prop="address" label="报修地址" width="auto" />
    <el-table-column prop="state" label="审核状态" width="auto">
      <template #default="scoped">
        <p v-if="scoped.row.state" style="color: green;">已通过</p>
        <p v-else  style="color: red;">未通过</p>
      </template>
    </el-table-column>
    <el-table-column  label="操作" width="auto">
      <template #default="scoped">
        <el-button plain type="primary" @click="dialogShow(scoped.row.id)">查看报修详情</el-button>
        <el-button v-if="!scoped.row.state" @click="changeState(scoped.row.id,scoped.row.state,'alw')" plain type="primary">确认维修</el-button>
        <el-button v-if="scoped.row.state" @click="changeState(scoped.row.id,scoped.row.deleted,'del')" plain type="danger">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
    <el-pagination 
    style="margin-top: 10px;" 
    background
    :page-count="tableData.pages"
    v-model:current-page="params.pageNo"
    @current-change="getTableData"
    :total="tableData.total">
  </el-pagination>
  <el-dialog 
      draggable
      :model-value="dialog.show"
      width="1000px"
      :before-close="handleClose"
      :close-on-click-modal="false">
     <Detail
      v-if="dialog.show"
      @getTableData="getTableData"
      v-model:show="dialog.show"
      :id="dialog.id"
     />
  </el-dialog>
</template>

<script  setup>
import { _axios,request,get,post } from '@/plugins/axios' 
import { ref,reactive } from 'vue'
import path from './path'
import Detail from './detail.vue';
let tableData = ref({})
// 分页的数据，作为分页查询get的参数
const params = reactive({
  pageNo : 1,
  pageSize: 13
})
const findparams = reactive({
  keyword:null,
  order: false,
  part: false
})
const dialog = reactive({
  detailshow:false,
  show:false,
  title:'',
  id:null,
});
const options = ref([
  {
    value: null,
    label: '全部显示'
  },
  {
    value: false,
    label: '仅显示已维修'
  },
  {
     value: true,
    label: '仅显示未维修'
  }
])
// 页面渲染的时候先获取一次，初始化
getTableData()
// 获取数据
function getTableData(){
  if(findparams.keyword != null){
    params.keyword = findparams.keyword
  }
  if(findparams.order){
    params.order = true
  } else params.order = false
  if(findparams.part){
    params.part = true
  } else if(findparams.part == null){
    params.part = null
  } else params.part = false
  get(path.list,params,content => {
    tableData.value = content
  })
}
function changeOrder(){
  findparams.order = !findparams.order
  getTableData()
}
function changeState(id,state,key){
  let text = key == 'alw' ? '您确定已经维修过了么，一但确认将无法修改！' : '您确定要删除么，一但删除将无法恢复！';
  ElMessageBox.confirm(text,'警告',{
    type:'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  }).then(()=>{
      post(path.changeState,{id,state,key},content =>{
        getTableData()
      })
  }).catch(()=>{})
}
function dialogShow(id){
    dialog.id = id
    dialog.show = true
}
// 对话框关闭事件
function handleClose(){
    // 将表单隐藏
    dialog.id = null
    dialog.show = false
} 
</script>

<style scoped lang='scss'>

</style>